package com.ys.service.mall.impl;

import java.util.Date;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bo.vo.R;
import com.ys.dto.AddDto;
import com.ys.dto.EditDto;
import com.ys.entity.mall.TmGoods;
import com.ys.entity.mall.TmGoodsStockRunning;
import com.ys.enume.mall.TmGoodsStockRunningEnum;
import com.ys.mapper.mall.MallMapper;
import com.ys.mapper.mall.TmGoodsMapper;
import com.ys.mapper.mall.TmGoodsStockRunningMapper;
import com.ys.service.mall.TmGoodsService;
import com.ys.util.DateUtil;

@Service("tmGoodsService")
public class TmGoodsServiceImpl implements TmGoodsService {

	@Autowired
	private MallMapper mallMapper;
	@Autowired
	private TmGoodsMapper tmGoodsMapper;
	@Autowired
	private TmGoodsStockRunningMapper tmGoodsStockRunningMapper;

	@Override
	public String generateGoodsCode() {
		String today = DateUtil.getYYYY_MM_DD(new Date());
		Integer num = new TmGoods()
				.where("A.CreateTime >= '" + today + " 00:00:00'")
				.where("A.CreateTime <= '" + today + " 23:59:59'")
				.count() + 1;
		
		return "YXT" + (today.substring(2).replace("-", "")) + (num > 9 ? num : ("0" + num));
	}


	@Override
	public R add(Map<String, Object> params) {
		
		// 验证库存数量
		if (!params.containsKey("stockNum")) {
			return R.error("库存数量不能为空");
		}
		Integer stockNum = Integer.parseInt(params.get("stockNum").toString());
		if (stockNum < 0) {
			return R.error("库存数量不能为负");
		}
		
		// 获取排序
		Integer sortNo = mallMapper.getNextSortNoOfTmGoods();
		
		TmGoods entity = JSON.parseObject(JSON.toJSONString(params), TmGoods.class);
		entity.setSortNo(sortNo);
		tmGoodsMapper.insert(entity);
		
		// 添加库存流水记录
		TmGoodsStockRunning tmGoodsStockRunning = null;
		if(stockNum > 0) {
			Long goodsId = entity.getId();
			String goodsCode = entity.getCode();
			String goodsName = entity.getName();
			Integer type = TmGoodsStockRunningEnum.Type.GOODS_ADD.value;
			Long relativeId = entity.getId();
			Integer numChange = stockNum;
			Integer numLeft = stockNum;
			String remark = "商品发布时入库";
			tmGoodsStockRunning = TmGoodsStockRunning.tmGoodsStockRunning(goodsId, goodsCode, goodsName, type, relativeId, numChange, numLeft, remark, 1);
		}
		tmGoodsStockRunningMapper.insert(tmGoodsStockRunning);
		
		return R.ok(AddDto.closeAndReload());
	}
	
	@Override
	public R edit(Map<String, Object> params, Long id, Integer numChange) {
		
		// 验证库存数量
		numChange = numChange == null ? 0 : numChange;
		TmGoods old = new TmGoods().setId(id).queryFirst();
		if (old.getStockNum() + numChange < 0) {
			return R.error("库存变动数量与现有库存数量总和不得低于0");
		}
		
		// 验证编码
		String code = params.get("code").toString();
		if (new TmGoods().setCode(code).where("A.Id != '" + params.get("id").toString() + "'").count() > 0) {
			return R.error("商品已存在");
		}

		TmGoods entity = JSON.parseObject(JSON.toJSONString(params), TmGoods.class);
		entity.setCode(null)
			.setStockNum(old.getStockNum() + numChange);
		tmGoodsMapper.update(entity);

		// 添加库存流水
		if (numChange != 0) {
			Long goodsId = entity.getId();
			String goodsCode = entity.getCode();
			String goodsName = entity.getName();
			Integer type = numChange > 0 ? TmGoodsStockRunningEnum.Type.GOODS_EDIT_IN.value : TmGoodsStockRunningEnum.Type.GOODS_EDIT_OUT.value;
			Long relativeId = entity.getId();
			Integer numLeft = entity.getStockNum();
			String remark = numChange > 0 ? "编辑商品时入库" : "编辑商品时出库";
			TmGoodsStockRunning tmGoodsStockRunning = TmGoodsStockRunning.tmGoodsStockRunning(goodsId, goodsCode, goodsName, type, relativeId, numChange, numLeft, remark, 1);
			tmGoodsStockRunningMapper.insert(tmGoodsStockRunning);
		}
		
		return R.ok(EditDto.closeAndReload());
	}

	@Override
	@Transactional
	public R moveTop(Long id) throws Exception {
		mallMapper.moveTopOfTmGoods(id);
		return R.ok();
	}

	@Override
	@Transactional
	public R resort(String rows) {
		JSONArray JA = JSON.parseArray(rows);
		
		for (int i = 0; i < JA.size(); i++) {
			JSONObject JO = JA.getJSONObject(i);
			Long id = JO.getLong("id");
			Integer sortNo = i + 1;
			mallMapper.updateTmGoodsOfSortNo(id, sortNo);
		}
		return R.ok();
	}

	@Override
	@Transactional
	public void changeNum(Long goodsId, Integer type, Integer numChange, String remark, Long relativeId) throws Exception {

		// 变更数量
		TmGoods entity = new TmGoods().setId(goodsId).queryFirst();
		entity.setStockNum(entity.getStockNum() + numChange);
		tmGoodsMapper.update(entity);

		// 添加库存流水
		String goodsCode = entity.getCode();
		String goodsName = entity.getName();
		Integer numLeft = entity.getStockNum();
		TmGoodsStockRunning tmGoodsStockRunning = TmGoodsStockRunning.tmGoodsStockRunning(goodsId, goodsCode, goodsName, type, relativeId, numChange, numLeft, remark, 1);
		tmGoodsStockRunningMapper.insert(tmGoodsStockRunning);
	}
}
